/*
 *  Class name: SoccerMatchDataModel.java
 *  Version: 1.0
 *  Date: 22.3.2014
 */
package cz.muni.fi.pa165.soccergamesarchive.datamodels.entity;

import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.SoccerMatchHeaderDTO;
import cz.muni.fi.pa165.soccergamesarchive.businesslayer.services.SoccerMatchServiceBeanLocal;
import cz.muni.fi.pa165.soccergamesarchive.datamodels.EntityDataModel;
import java.util.List;
import java.util.Map;
import org.richfaces.component.SortOrder;

import static java.lang.Long.parseLong;

/**
 *
 * @author Michal Ďuriš
 */
public class SoccerMatchDataModel extends EntityDataModel<SoccerMatchHeaderDTO> {

    private final SoccerMatchServiceBeanLocal matchServiceBean;

    private Map<String, String> filterValuesMap = null;
    private Map<String, SortOrder> sortOrdersMap = null;

    public SoccerMatchDataModel(SoccerMatchServiceBeanLocal matchServiceBean) {
        this.matchServiceBean = matchServiceBean;

        orderMap.put("matchDate", Boolean.FALSE);
    }

    public void setFilterValuesMap(Map<String, String> filterValuesMap) {
        this.filterValuesMap = filterValuesMap;
    }

    public void setSortOrdersMap(Map<String, SortOrder> sortOrdersMap) {
        this.sortOrdersMap = sortOrdersMap;
    }

    private void copyFilterValuesToFilterMap() {

        if (filterValuesMap != null && !filterValuesMap.isEmpty()) {
            filterMap.putAll(filterValuesMap);
        }
    }

    @Override
    protected List<SoccerMatchHeaderDTO> getArrangedList(int offset, int limit) {

        copyFilterValuesToFilterMap();

        return matchServiceBean.getLimitedNumberOfMatchHeaders(offset, limit,
                getCompleteFilterMap(), getOrderMap());
    }

    @Override
    protected int getAllArrangedItemsCount() {

        copyFilterValuesToFilterMap();

        return matchServiceBean.getFilteredMatchHeadersCount(
                getCompleteFilterMap());
    }

    @Override
    protected SoccerMatchHeaderDTO getEntityByID(Object id) {

        SoccerMatchHeaderDTO matchHeader = null;
        long key = -1;
        int index = -1;

        if (id instanceof String) {
            key = parseLong((String) id);
        } else if (id instanceof Long) {
            key = (long) id;
        } else if (id instanceof Integer) {
            index = (int) id;
        } else {
            //TODO: CHANGE TO LOGGER
            System.err.println(getClass().getSimpleName()
                    + "->getEntityByID->"
                    + "ID has uncorrect type: "
                    + "ID class: " + id.getClass()
                    + "ID value: " + id);
            return null;
        }

        if (index >= 0 && index < dataList.size()) {

            matchHeader = dataList.get(index);
        } else if (key > 0 && key <= dataList.size()) {

            matchHeader = dataList.get((int) (key - 1));
        } else {
            //TODO: CHANGE TO LOGGER
            System.err.println(getClass().getSimpleName()
                    + "->getEntityByID->"
                    + "Item not found. Wrong id! "
                    + "ID value: " + id);
        }

        return matchHeader;
    }

    @Override
    protected Long getID(SoccerMatchHeaderDTO entity) {
        return entity.getId();
    }

}
